﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HenIT.Data.SqlClient;
using System.Data.SqlClient;
using System.Data;
namespace EventScavenger
{
	public partial class EventScavengerDAL : GenericSQLServerDAL
	{
		#region Collectors
		public List<Collector> GetCollectorList()
		{
			List<Collector> collectors = new List<Collector>();
			string sql = "SelectCollectors";
			DataSet colds = GetDataSet(sql, CommandType.StoredProcedure);
			if (colds.Tables.Count > 0 && colds.Tables[0].Rows.Count > 0)
			{
				collectors.AddRange(
						(from DataRow r in colds.Tables[0].Rows
						 select new Collector()
						 {
								Id = (int)r["CollectorID"],
								Name = r["Name"].ToString(),
								MainThreadUpdateFrequencySec = (int)r["MainThreadUpdateFrequencySec"],
								UseBatchInserts = (bool)r["UseBatchInserts"],
								InsertBatchSize = (int)r["InsertBatchSize"],
								SqlCommandTimeOutSec = (int)r["SqlCommandTimeOutSec"],
								LogDuplicateWarnings = (bool)r["LogDuplicateWarnings"],
								ImportsEnabled = (bool)r["ImportsEnabled"],
								ImportFrequency = (int)r["ImportFrequencyMinutes"],
                                ReaperId = (int)r["ReaperID"],
                                ReaperName = N(r["ReaperName"]),
                                Enabled = (bool)r["Enabled"],
                                ServiceName = N(r["ServiceName"]),
                                ServicePath = N(r["ServicePath"])
						 }
						)
						);
			}
			return collectors;
		}
		public void UpdateCollector(Collector collector)
		{
			string sql = "UpdateCollector";
			SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@CollectorID", collector.Id),
					new SqlParameter("@Name", collector.Name),
					new SqlParameter("@MainThreadUpdateFrequencySec", collector.MainThreadUpdateFrequencySec),
					new SqlParameter("@UseBatchInserts", collector.UseBatchInserts),
					new SqlParameter("@InsertBatchSize", collector.InsertBatchSize),
					new SqlParameter("@SqlCommandTimeOutSec", collector.SqlCommandTimeOutSec),
					new SqlParameter("@LogDuplicateWarnings", collector.LogDuplicateWarnings),
					new SqlParameter("@ImportsEnabled", collector.ImportsEnabled),
					new SqlParameter("@ImportFrequencyMinutes", collector.ImportFrequency),
                    new SqlParameter("@ReaperID", collector.ReaperId),
                    new SqlParameter("@Enabled", collector.Enabled),
                    new SqlParameter("@ServiceName", collector.ServiceName),
                    new SqlParameter("@ServicePath", collector.ServicePath)
				};
			ExecuteSP(sql, parameters);
		}
		public void InsertCollector(Collector collector)
		{
			string sql = "InsertCollector";
			SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@Name", collector.Name),
					new SqlParameter("@MainThreadUpdateFrequencySec", collector.MainThreadUpdateFrequencySec),
					new SqlParameter("@UseBatchInserts", collector.UseBatchInserts),
					new SqlParameter("@InsertBatchSize", collector.InsertBatchSize),
					new SqlParameter("@SqlCommandTimeOutSec", collector.SqlCommandTimeOutSec),
					new SqlParameter("@LogDuplicateWarnings", collector.LogDuplicateWarnings),
					new SqlParameter("@ImportsEnabled", collector.ImportsEnabled),
					new SqlParameter("@ImportFrequencyMinutes", collector.ImportFrequency),
                    new SqlParameter("@ReaperID", collector.ReaperId),
                    new SqlParameter("@Enabled", collector.Enabled),
                    new SqlParameter("@ServiceName", collector.ServiceName),
                    new SqlParameter("@ServicePath", collector.ServicePath)
				};
			ExecuteSP(sql, parameters);
		}
		public void DeleteCollector(Collector collector)
		{
			string sql = "DeleteCollector";
			SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@CollectorID", collector.Id)
				};
			ExecuteSP(sql, parameters);
		}
		#endregion
	}
}
